package com.boot.mall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.boot.common.constant.MqDestinationConstant;
import com.boot.common.to.HasStockTo;
import com.boot.common.to.order.OrderStatusTo;
import com.boot.mall.ware.exception.NotStackException;
import com.boot.mall.ware.feign.MemberFeignService;
import com.boot.mall.ware.feign.OrderFeignService;
import com.boot.mall.ware.mq.JmsProducerService;
import com.boot.mall.ware.vo.SkuStockNum;
import com.boot.mall.ware.vo.WareSkuLockVo;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.*;

import com.boot.mall.ware.entity.WareSkuEntity;
import com.boot.mall.ware.service.WareSkuService;
import com.boot.common.utils.PageUtils;
import com.boot.common.utils.R;

import javax.jms.JMSException;


/**
 * 商品库存
 *
 * @author huihui
 * @email 2565036109@qq.com
 * @date 2021-05-06 19:36:17
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    @Autowired
    JmsProducerService jmsProducerService;
    @Autowired
    OrderFeignService orderFeignService;
    @Autowired
    MemberFeignService memberFeignService;

    /**
     * 锁定订单的订单项的数量
     * @param vo
     * @return
     */
    @PostMapping("/lock/sku/stock")
    public R lockSkuStock(@RequestBody WareSkuLockVo vo){
        try {
            wareSkuService.lockSkuStock(vo);
            return R.ok();
        }catch (NotStackException e){
            return R.error();
        }
    }

    /**
     * 测试发送消息
     * @return
     */
    @GetMapping("/send")
    @ResponseBody
    public String test2(){
        SkuStockNum skuStockNum = new SkuStockNum();
        skuStockNum.setWareId(1L);
        skuStockNum.setNum(100);
//        jmsProducerService.delaySendMessage(skuStockNum,10L);
        return "发送成功";
    }

    /**
     * 查询sku商品是否还有库存
     * @param skuIds
     * @return
     */
    @PostMapping("/hasstock")
    public List<HasStockTo> hasStock(@RequestBody List<Long> skuIds){
        List<HasStockTo> hasStockTos = wareSkuService.hasStock(skuIds);
        return hasStockTos;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils pageUtils = wareSkuService.pageSku(params);
        return R.ok().put("page",pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
